package cn.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @auth wzy
 * @date 2023/9/26 16:16
 */
public class Test2 {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
//        new Person();

        Class pc = Person.class;

        Person person = (Person)pc.newInstance();

        System.out.println("person.toString() = " + person.toString());

        Constructor constructor = pc.getConstructors()[0];
        Person p2 = (Person)constructor.newInstance();

        System.out.println("p2.toString() = " + p2.toString());

        Constructor declaredConstructor = pc.getDeclaredConstructor(String.class);
        declaredConstructor.setAccessible(true);

        Person p3 = (Person)declaredConstructor.newInstance("aaa");
        System.out.println("p3.toString() = " + p3.toString());
    }
}
